//
// Created by 20792 on 24-8-20.
//

#include "login.h"

//json组包
char* json_pack(USE* user_info) {//传过来是指针了!!!
    cJSON* json = cJSON_CreateObject();
    cJSON_AddNumberToObject(json, "user_id", user_info->user_id);
    cJSON_AddStringToObject(json, "user_name", user_info->user_name);
    cJSON_AddStringToObject(json, "user_password", user_info->user_password);
    cJSON_AddNumberToObject(json, "user_type", user_info->user_type);
    cJSON_AddNumberToObject(json, "type", user_info->type);
    char *dest = cJSON_Print(json);
    cJSON_Delete(json);
    return dest;
}

// 登录 -- 返回1为登陆成功    -- 返回0为登陆失败
USE* Login(USE* user_info,int sock)
{
    int ret = 0;
    char *dest = NULL;
    printf("请输入账号：\n");
    scanf("%s", user_info->user_name);
    printf("请输入密码：\n");
    scanf("%s", user_info->user_password);
    dest = json_pack(user_info);
    printf("dest:%s",dest);
    puts(dest);
    ret = send(sock, dest, 1024, 0);
    if (ret == -1) {
        perror("send");
        return 0;
    }
    printf("发送成功");
    return user_info;
}
